package com.cow.framework.core.config.dict;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.cow.framework.core.cache.RedisUtil;
 
/**
 * 数据字典初始化
 *
 * @author yangsj
 */
@Component
public class DictSchedule{
	@Autowired
	protected RedisUtil redisUtil;
	@Autowired
	DictDAO dictDAO;
	
	@Scheduled(initialDelay=1000, fixedRate=3600000) 
    public void init(){
		Sort sort = new Sort(Direction.ASC, "type","value");
    	List<Dict> dictList = dictDAO.findAll(sort);
    	Map<String,Map<String,Dict>> app_dictMap = new HashMap<String,Map<String,Dict>>();
    	Map<String,Map<String,String>> sys_dictMap = new HashMap<String,Map<String,String>>();
    	for(Dict d:dictList){
    		//app
    		Map<String,Dict> dictMap = (Map<String,Dict>)app_dictMap.get(d.getType());
    		if(dictMap == null){
    			dictMap = new LinkedHashMap<String,Dict>();
    			app_dictMap.put(d.getType(), dictMap);
    		}
    		dictMap.put(d.getValue(), d);    	
    		//sys
    		Map<String,String> dictNameMap = (Map<String,String>)sys_dictMap.get(d.getType());
    		if(dictNameMap == null){
    			dictNameMap = new LinkedHashMap<String,String>();
    			sys_dictMap.put(d.getType(), dictNameMap);
    		}
    		dictNameMap.put(d.getValue(), d.getValueName()); 
    	}
    	
    	redisUtil.set("sys_dictMap", sys_dictMap);
    	redisUtil.set("app_dictMap", app_dictMap);
    }
}
